package com.lyh.crm.workbench.service.impl;

import com.lyh.crm.settings.dao.UserDao;
import com.lyh.crm.settings.domain.User;
import com.lyh.crm.utils.DateTimeUtil;
import com.lyh.crm.utils.SqlSessionUtil;
import com.lyh.crm.utils.UUIDUtil;
import com.lyh.crm.vo.PaginationVO;
import com.lyh.crm.workbench.dao.*;
import com.lyh.crm.workbench.domain.*;
import com.lyh.crm.workbench.service.TranService;
import com.lyh.crm.workbench.service.VisitService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author 北京动力节点
 */
public class VisitServiceImpl implements VisitService {

    private VisitDao visitDao = SqlSessionUtil.getSqlSession().getMapper(VisitDao.class);
    private VisitRemarkDao visitRemarkDao = SqlSessionUtil.getSqlSession().getMapper(VisitRemarkDao.class);

    private TranDao tranDao = SqlSessionUtil.getSqlSession().getMapper(TranDao.class);

    private ContactsDao contactsDao = SqlSessionUtil.getSqlSession().getMapper(ContactsDao.class);

    public PaginationVO<Visit> pageList(Map<String, Object> map) {

        //取得total
        int total = visitDao.getTotalByCondition(map);
        System.out.println("total = " + total);

        String stage = "07成交";

        List<Tran> tranList = tranDao.getTranList(stage);

        if (tranList != null && tranList.size() > 0) {

            Visit v = null;

            for (Tran tran : tranList) {

                String tranId = tran.getId();
                int count = visitDao.getTotalByTranId(tranId);
                System.out.println(count);
                if (count==0){
                    String contactsId = tran.getContactsId();
                    System.out.println("contactsId = " + contactsId);

                    v = new Visit();
                    v.setId(UUIDUtil.getUUID());
                    v.setOwner(tran.getOwner());
                    v.setSubject(tran.getName());
                    v.setExpiryDate(tran.getExpectedDate());
                    v.setContactsId(contactsId);
                    v.setState("");

                    int count1 = visitDao.getContactsIdTotal(contactsId);

                    if (count1!=0){
                        String priority = visitDao.getPriorityByContactsId(contactsId);
                        v.setPriority(priority);
                    }else {
                        v.setPriority("常规");
                    }

                    v.setDescription(tran.getDescription());
                    v.setStartTime(tran.getCreateTime());
                    v.setRepeatType("");
                    v.setNoticeType("");
                    v.setCreateTime(DateTimeUtil.getSysTime());
                    v.setCreateBy(tran.getCreateBy());
                    v.setTranId(tranId);
                    int count2 = visitDao.save(v);
                    if (count2 != 1) {
                        System.out.println("========出错了！！！========");
                        return null;
                    }
                }

            }
        }

        //取得dataList
        List<Visit> dataList = visitDao.getVisitListByCondition(map);

        for (Visit visit : dataList) {
            System.out.println("visit = " + visit);
        }

        //创建一个vo对象，将total和dataList封装到vo中
        PaginationVO<Visit> vo = new PaginationVO<Visit>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        //将vo返回
        return vo;
    }

    public boolean save(Visit v) {

        boolean flag = true;

        //添加交易
        int count2 = visitDao.save(v);
        if (count2 != 1) {
            flag = false;
        }

        return flag;
    }

    public Visit getAllById(String id) {

        Visit v = visitDao.getAllById(id);

        return v;
    }

    public boolean deleteByIds(String[] ids) {

        boolean flag = true;

        //查询出需要删除的备注的数量
        int count1 = visitRemarkDao.getCountByAids(ids);

        //删除备注，返回受到影响的条数（实际删除的数量）
        int count2 = visitRemarkDao.deleteByAids(ids);

        if (count1 != count2) {

            flag = false;

        }

        //删除交易
        int count3 = visitDao.deleteByIds(ids);
        if (count3 != ids.length) {

            flag = false;

        }

        return flag;
    }

    public boolean update(Visit t) {

        boolean flag = true;

        int count = visitDao.update(t);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public Visit detail(String id) {
        Visit v = visitDao.detail(id);

        return v;
    }

    public List<VisitRemark> getRemarkListByAid(String visitId) {

        List<VisitRemark> vrList = visitRemarkDao.getRemarkListByAid(visitId);

        return vrList;
    }

    public boolean deleteRemark(String id) {

        boolean flag = true;

        int count = visitRemarkDao.deleteById(id);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean saveRemark(VisitRemark vr) {

        boolean flag = true;

        int count = visitRemarkDao.saveRemark(vr);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

    public boolean updateRemark(VisitRemark vr) {

        boolean flag = true;

        int count = visitRemarkDao.updateRemark(vr);

        if (count != 1) {

            flag = false;

        }

        return flag;
    }

}


































